package com.tripwego.client.ui.widget.activite;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasMouseOutHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.tripwego.client.model.ActiviteModel;
import com.tripwego.client.ui.pageVoyage.IVoyageView;
import com.tripwego.client.ui.widget.DayWidget;
import com.tripwego.client.ui.widget.FlexTableHelper;

public class RentalCheckinWidget extends Composite implements IVoyageView, ClickHandler, HasMouseOverHandlers, HasMouseOutHandlers {

	private Presenter presenter;

	private ActiviteModel activiteModel = null;

	private DayWidget dayWidget = null;

	/*
	 * Elements graphiques
	 */
	private FlexTable viewPanel = new FlexTable();

	private Image image_step = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/pin_down.png");

	private Image image_update = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/settings.png");

	private Image image_delete = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/close.png");

	private HorizontalPanel panel_actions = new HorizontalPanel();

	private final HTML html_title = new HTML("Name - Type - Arriv\u00E9e");

	private final HTML html_detail = new HTML("Arriv\u00E9e\r\n<br>D\u00E9part\r\n<br>Nuits", true);

	/**
	 * 
	 */
	public RentalCheckinWidget(DayWidget dayWidget, ActiviteModel activiteModel) {
		super();

		this.dayWidget = dayWidget;
		this.activiteModel = activiteModel;

		viewPanel.setStyleName("ActiviteHeaderWidget");
		viewPanel.setSize("350px", "");

		initWidget(viewPanel);

		Image image_icon = new Image(activiteModel.getIconUrl());
		viewPanel.setWidget(0, 0, image_icon);
		viewPanel.getCellFormatter().setWidth(0, 0, "1%");
		image_icon.setSize("24px", "24px");

		viewPanel.setWidget(0, 1, html_title);
		viewPanel.getCellFormatter().setWidth(0, 1, "100%");
		panel_actions.setStyleName("horizontalPanel_actions");
		panel_actions.setSpacing(2);

		viewPanel.setWidget(0, 2, panel_actions);
		panel_actions.setWidth("1%");

		panel_actions.add(image_step);
		image_step.setTitle("Point d'arr\u00EAt sur la carte");

		panel_actions.add(image_update);
		image_update.setTitle("Modifier l'activit\u00E9");
		panel_actions.add(image_delete);

		image_delete.setTitle("Supprimer l'activit\u00E9");
		viewPanel.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_TOP);
		viewPanel.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_RIGHT);

		image_delete.addClickHandler(this);

		image_step.setStyleName("img_action");
		image_update.setStyleName("img_action");
		image_delete.setStyleName("img_action");

		viewPanel.setWidget(1, 1, html_detail);
		viewPanel.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		viewPanel.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
		viewPanel.getFlexCellFormatter().setRowSpan(0, 0, 2);
		viewPanel.getFlexCellFormatter().setRowSpan(0, 2, 2);
		FlexTableHelper.fixRowSpan(viewPanel);
		viewPanel.getFlexCellFormatter().setRowSpan(0, 0, 2);

		addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				majPanelActions(false);
			}
		});

		addMouseOutHandler(new MouseOutHandler() {

			@Override
			public void onMouseOut(MouseOutEvent event) {
				majPanelActions(true);
			}
		});

		String title = activiteModel.getNom().concat(" - Checkin");
		html_title.setHTML("<b>".concat(title).concat("</b>"));

		DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("EEE, MMM d - HH:mm");

		StringBuilder sb_detail = new StringBuilder();
		sb_detail.append("Pickup : ").append(dateTimeFormat.format(activiteModel.getDateDeb()));
		sb_detail.append("<br>Return : ").append(dateTimeFormat.format(activiteModel.getDateFin()));
		sb_detail.append("<br>").append(activiteModel.getNbDay()).append(" jour(s)");

		html_detail.setHTML(sb_detail.toString());
	}

	@Override
	public void onClick(ClickEvent event) {

		event.stopPropagation();

		Object sender = event.getSource();

		// if (sender == image_delete) {
		// SC.ask("Etes-vous sur de vouloir supprimer l'activite ?", new
		// BooleanCallback() {
		//
		// @Override
		// public void execute(Boolean value) {
		// if (value) {
		// activiteWidget.removeFromParent();
		// presenter.onDeleteActivite(activiteModel);
		// }
		// }
		// });
		// }
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setEventBus(EventBus eventBus) {
		// TODO Auto-generated method stub

	}

	@Override
	public void init() {
		majPanelActions(true);
	}

	@Override
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	public void majPanelActions(boolean disabled) {
		image_step.setStyleDependentName("disabled", disabled);
		image_update.setStyleDependentName("disabled", disabled);
		image_delete.setStyleDependentName("disabled", disabled);
	}
}
